package Test;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PersonJPanel extends JPanel implements ActionListener {
    private JTextField name, data;
    private JRadioButton[] radios;
    public JComboBox<String> province, city;
    private static String[] provinces = {"河北","山西","江苏"/*,"黑龙江","吉林","辽宁","浙江","安徽","福建","江西","山东"
    ,"河南","湖北","湖南","广东","海南","四川","贵州","云南","陕西","甘肃","青海","台湾",
    "内蒙古","广西","西藏","宁夏","新疆","北京","天津","上海","重庆","香港","澳门"*/};
    private static String[][] cities = {{"石家庄", "唐山", "秦皇岛", "邯郸", "邢台", "保定", "张家口", "承德", "沧州", "廊坊", "衡水"},
            {"太原","临汾","朔州","大同","长治","吕梁","晋中","忻州","运城","阳泉","晋城"},
            {"苏州","无锡","常州","镇江","南京","南通","扬州","泰州","盐城","淮安","宿迁","徐州","连云港"}};
    public PersonJPanel() {
        this.setBorder(new TitledBorder("Person"));//带标题边框
        this.setLayout(new GridLayout(5,1));
        this.add(this.name = new JTextField("姓名"));
        this.add(this.data = new JTextField("2023年6月1日"));
        String[] str = {"男","女"};
        JPanel jPanel = new JPanel(new GridLayout(1,2));
        this.add(jPanel);
        ButtonGroup buttonGroup = new ButtonGroup();
        this.radios = new JRadioButton[str.length];
        for (int i = 0; i < radios.length; i++) {
            jPanel.add(this.radios[i] = new JRadioButton(str[i]));//单选按钮，默认不选中
            buttonGroup.add(this.radios[i]);//添加到按钮组
        }
        this.radios[0].setSelected(true);//默认选中第一个
        this.add(this.province = new JComboBox<>(PersonJPanel.provinces));
        this.add(this.city = new JComboBox<>(PersonJPanel.cities[0]));
        this.province.addActionListener(this);
    }
    public void set(Person per){
        if(per == null){
            return;
        }
        this.name.setText(per.name);
        this.data.setText(per.data);
        if(per.sex.equals("男")){
            this.radios[0].setSelected(true);
        }else {
            this.radios[1].setSelected(true);
        }
        this.province.setSelectedItem(per.province);
        this.city.setSelectedItem(per.city);
    }
    public Person get(){
        String sex = radios[0].isSelected() ? radios[0].getText() : radios[1].getText();
        try {
            MyDate birthday = new MyDate(this.data.getText());
            return new Person(name.getText(),birthday.toString(),sex,
                    (String)province.getSelectedItem(),
                    (String)city.getSelectedItem());
        }catch (NumberFormatException e){
            JOptionPane.showMessageDialog(this,e.getMessage()
            +"字符串不能转换成整数");
        }catch (DateFormatException e){
            JOptionPane.showMessageDialog(this,e.getMessage());
        }
        return null;
    }
    public void actionPerformed(ActionEvent event){
        int i = this.province.getSelectedIndex();
        if(cities != null && i != -1){
            this.city.removeAllItems();
            for (int j = 0; j < PersonJPanel.cities[i].length; j++) {
                this.city.addItem(PersonJPanel.cities[i][j]);
            }
        }
    }
}
